package com.kmxd.ams.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kmxd.ams.client.system.ISysDictSvc;
import com.kmxd.ams.client.system.dto.SysDictAddDTO;
import com.kmxd.ams.client.system.dto.SysDictEditDTO;
import com.kmxd.ams.client.system.dto.SysDictQueryDTO;
import com.kmxd.ams.client.system.vo.SysDictListVO;
import com.kmxd.ams.client.system.vo.SysDictVO;
import com.kmxd.ams.core.common.bo.DictModel;
import com.kmxd.ams.core.common.enums.DictValueEnum;
import com.kmxd.ams.core.common.enums.LogLevelEnum;
import com.kmxd.ams.core.common.enums.LogTypeEnum;
import com.kmxd.ams.core.log.annotation.AutoLog;
import com.kmxd.ams.core.result.R;
import com.kmxd.ams.core.util.SecurityUtil;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 字典表 前端控制器
 *
 * @author admin
 * @since 2021-06-10
 */
@Tag(name = "数据字典")
@RestController
@RequestMapping("/system/sysDict")
@AllArgsConstructor
public class SysDictCtl {

  private ISysDictSvc sysDictSvc;

  /** 分页查询字典列表 */
  @GetMapping("/selectSysDictWithPage")
  @SaCheckPermission("system:sysDict:selectSysDictWithPage")
  @AutoLog(module = "字典管理", title = "分页查询字典列表")
  public R<Page<SysDictListVO>> selectSysDictWithPage(Page reqPage, SysDictQueryDTO req) {
    return R.success(sysDictSvc.selectSysDictWithPage(reqPage, req));
  }

  /** 分页查询 */
  @GetMapping("/selectPage")
  @AutoLog(module = "字典管理", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.LOW, title = "分页查询")
  public R<Page<SysDictListVO>> selectPage(Page reqPage, SysDictQueryDTO req) {
    req.setComId(SecurityUtil.getCropId());
    return R.success(sysDictSvc.selectPage(reqPage, req));
  }

  /** 获取字典列表 */
  @GetMapping("/selectList")
  @AutoLog(module = "字典管理", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.LOW, title = "获取字典列表")
  public R<List<SysDictListVO>> selectList(SysDictQueryDTO req) {
    req.setComId(SecurityUtil.getCropId());
    return R.success(sysDictSvc.selectList(req));
  }

  /** 新增 */
  @PostMapping("/add")
  @SaCheckPermission("system:sysDict:add")
  @AutoLog(module = "字典管理", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.LOW, title = "新增")
  public R<Void> add(@Validated @RequestBody SysDictAddDTO req) {
    req.setComId(SecurityUtil.getCropId());
    sysDictSvc.add(req);
    return R.success();
  }

  /** 修改 */
  @PostMapping("/edit")
  @SaCheckPermission("system:sysDict:edit")
  @AutoLog(module = "字典管理", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.HIGH, title = "修改")
  public R<Void> edit(@Validated @RequestBody SysDictEditDTO req) {
    req.setComId(SecurityUtil.getCropId());
    sysDictSvc.edit(req);
    return R.success();
  }

  /** 删除 */
  @PostMapping("/remove")
  @SaCheckPermission("system:sysDict:remove")
  @AutoLog(module = "字典管理", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.HIGH_RISK, title = "删除")
  public R<Void> remove(@RequestParam String ids) {
    sysDictSvc.remove(ids);
    return R.success();
  }

  /** 查看 */
  @GetMapping("/view")
  @AutoLog(module = "字典管理", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.LOW, title = "查看")
  public R<SysDictVO> view(@RequestParam Long id) {
    return R.success(sysDictSvc.view(id));
  }

  /** 导出 */
  @GetMapping("/export")
  @SaCheckPermission("system:sysDict:export")
  @AutoLog(module = "字典管理", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.HIGH, title = "导出")
  @SneakyThrows
  public void export(SysDictQueryDTO req, HttpServletResponse response) {
    String filename = "字典列表" + "_" + System.currentTimeMillis() + ".xlsx";
    response.setContentType("application/vnd.ms-excel");
    response.setCharacterEncoding("UTF-8");
    response.setHeader(
        "Content-Disposition",
        "attachment;filename=" + URLEncoder.encode(filename, StandardCharsets.UTF_8.name()));
    EasyExcel.write(response.getOutputStream())
        .head(SysDictListVO.class)
        .sheet("字典列表")
        .doWrite(sysDictSvc.selectList(req));
  }

  /** 根据字典编码，查询字典项列表 */
  @GetMapping("/listDictModel")
  @AutoLog(
      module = "字典管理",
      type = LogTypeEnum.SYSTEM,
      level = LogLevelEnum.LOW,
      title = "根据字典编码，查询字典项列表")
  public <T> R<List<DictModel<T>>> listDictModel(
      @RequestParam String dictCode,
      @RequestParam(required = false, defaultValue = "1") Integer type,
      @RequestParam(required = false) String setCode,
      @RequestParam(required = false) Integer year,
      @RequestParam(required = false) String ignoreValue) {

    return R.success(
        sysDictSvc.listDictModel(
            dictCode,
            setCode,
            year,
            DictValueEnum.getValue(type),
            ignoreValue,
            SecurityUtil.getCropId()));
  }

  /** 字典序列 */
  @GetMapping("/listDictSeries")
  @AutoLog(
      module = "字典管理",
      type = LogTypeEnum.SYSTEM,
      level = LogLevelEnum.LOW,
      title = "根据字典编码，查询字典项序列列表")
  public R<List<DictModel<String>>> listDictSerial(
      @RequestParam String dictCode, @RequestParam(required = false) Long comId) {
    return R.success(sysDictSvc.listDictSeries(dictCode, comId));
  }

  /** 刷新数据字典缓存 */
  @PostMapping("/refresh")
  @SaCheckPermission("system:sysDict:refresh")
  @AutoLog(module = "字典管理", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.LOW, title = "刷新数据字典缓存")
  public R<Void> refresh() {
    sysDictSvc.clearAllCache();
    return R.success();
  }
}
